#include <string.h>
#include <string>
#include <cstddef>
#include "pseDnsCacheer.h"

inline std::string trim(const std::string& s) {
  if(s.length() == 0)
    return s;
  std::size_t beg = s.find_first_not_of(" \a\b\f\n\r\t\v");
  std::size_t end = s.find_last_not_of(" \a\b\f\n\r\t\v");
  if(beg == std::string::npos) // No non-spaces
    return "";
  return std::string(s, beg, end - beg + 1);
}

int main(void){
    PseDnsCacheer dns;
    unsigned int  cache_size = 100000;
    unsigned int  hash_size = 100000;
    int           expired_seconds = 600;
    char         *filename = "report.dat";
    char         *cache_name = "DNS Cache";
    char         *ip = "www.kimo.com.tw";
    int           ip_len = strlen(ip);

/*
    if(dns.load_file(filename) == 0){
        printf("loading file is successfully\n");
    }

    return 0;
*/
    FILE *fp = NULL;
    char *str = NULL;
    char *value = "172.17.222.10";
    char  buf[1024];
    //fp = fopen("/home/eric/log/aalert_log_tokenize", "r");
    
    dns.set_expired_seconds(expired_seconds);
    
    if((dns.init(cache_size, hash_size)) == true){
        printf("Initializing is successful\n");
    }
    
    //fp = fopen("/home/eric/log/test_for_cache", "r");
    //fp = fopen("/home/eric/log/pivot_member_last.lst", "r");
    
    
    FILE *url_fp = fopen("/home/eric/pivot_search/test_data/master_dns_cache_file.txt", "r");
    if(url_fp == NULL){
        printf("could not open dir_url.txt file\n");
    }
    else{
        //char *line = NULL;        
        while(fgets(buf, 1024, url_fp) != NULL){
            char *url = NULL;
            char  *array[4];
            char *saveptr = NULL;
            url = strtok_r(buf, "\t", &saveptr);
            int i = 0;            
            do{
                if(*url == '#') break;
                array[i] = url;
                i++;                
            }while((url = strtok_r(NULL, "\t", &saveptr)) != NULL);           
            //printf("array[0]:%s, array[1]:%s, array[2]:%s, array[3]:%s\n", array[0], array[1],
            //       array[2], array[3]);                               
            if(dns.insert(array[0], strlen(array[0]), array[1], strlen(array[1])) == true){
                //printf("cached dns is successfully\n");
            }           
        }    
    }
    fclose(url_fp);
    
    char  url_buf[1024];
    fp = fopen("/home/eric/pivot_search/test_data/dir_url_nohttp.txt", "r"); 
    if(fp == NULL){
        printf("could not open file\n");
        exit(1);
    }
    else{
        /*
        while(fgets(url_buf, 1024, fp) != NULL){
            char *u = NULL;
            char  *url_array[4];
            char *saveptr = NULL;
            u = strtok_r(url_buf, "/", &saveptr);
            int i = 0;            
            do{
                //if(*url == '#') break;
                url_array[i] = test_trim(u);
                i++;                
            }while((u = strtok_r(NULL, "/", &saveptr)) != NULL);            
            printf("%s\n", url_array[1]);
        } 
        */        
        while((str = fgets(url_buf, 1024, fp)) != NULL){
            //str = test_trim(str);
            if(dns.insert(str, strlen(str), NULL, 0) == true){
                //printf("cached dns is successfully\n");
            }
        }        
    } 
    fclose(fp);
    dns.dump_report(cache_name, filename);
    /*
    if(dns.dump_report(cache_name, filename) == true){
        printf("cache dumped is successfully\n");
    }    
    */
    /* free the dns cache objects */    
    if(dns.free_item() == true){
        printf("free the object of dns cache is successfully\n");
    }
        
    return 0;
}
